//
//  EMConversationCellModel.swift
//  easeIM-swift
//
//  Created by yangjian on 2022/7/13.
//

import UIKit
import RxSwift
import HyphenateChat

class EMConversationCellModel: NSObject {

    var userInfoBag = DisposeBag()
    var rx_userInfo = BehaviorSubject<EMUserInfo>(value: EMUserInfo())

    var id = ""
    var headImageName = ""
    var headURLString = ""
    var name = ""
    var chatType = IMChatType.singleChat
//    var lastMessageContentSubject = BehaviorSubject(value: "");
    var conversation : EMConversation?
    var lastMessageContent = ""
    
    
    init(conversation : EMConversation){
        super.init()
        self.conversation = conversation
        self.id = conversation.conversationId
        
        var chatType = IMChatType.singleChat
        if conversation.type == EMConversationType.chat {
            chatType = .singleChat
        }else if conversation.type == EMConversationType.groupChat {
            chatType = .groupChat
        }else if conversation.type == EMConversationType.chatRoom {
            chatType = .chatRoomChat
        }else{}
        self.chatType = chatType
        self.config()
    }
    
    func config(){
        if chatType == .singleChat {
            if id == EMSYSTEMNOTIFICATIONID {
                self.name = "系统提示"
                self.headImageName = "systemNotify"
            }else{
                self.headImageName = "defaultAvatar"
                if let username = self.conversation?.conversationId {
                    let userInfo = UserInfoStore.share.fetchUserInfo(username: username)
                    var needFetchFromServer = true
                    if let username = userInfo.userId {
                        if username.count > 0 {
                            needFetchFromServer = false
                            self.rx_userInfo.onNext(userInfo)
                        }
                    }

                    if needFetchFromServer {
                        UserInfoStore.share.fetchUserInfosFromServer(usernames: [username])
                        EMIMHelper.share.userinfoUpdateFinishSubject.subscribe(onNext: {[unowned self] usernames in
                            if usernames.contains(username) {
                                DispatchQueue.main.async {
                                    self.rx_userInfo.onNext(UserInfoStore.share.fetchUserInfo(username: username))
                                }
                                self.userInfoBag = DisposeBag()
                            }
                            
                        }).disposed(by: userInfoBag)
                    }
                }
            }
        }else if chatType == .groupChat {
            let group = EMGroup(id: id)
            self.name = group?.groupName ?? "群id:\(id)"
            self.headImageName = "groupConversation"
        }else if chatType == .chatRoomChat {
            self.name = "聊天室:\(id)"
            self.headImageName = "chatroomConversation"
        }else{
        }
    }
    
    
    
    func newMessageContent() -> String{
        guard let conversation = conversation else{
            return "";
        }
        var content = ""
        if let body = conversation.latestMessage.body as? EMMessageBody {
            if(body.type == .text){
                content = (body as! EMTextMessageBody).text
            }else if body.type == .custom {
                content = (body as! EMCustomMessageBody).event
            }else if body.type == .video {
                content = "[视频]"
            }else if body.type == .voice {
                content = "[语音]"
            }else if body.type == .image {
                content = "[图片]"
            }else{
                content = "新消息"
            }
        }
        
        return content
    }
    
    
//    init(id : String , chatType : IMChatType) {
//        super.init()
//        self.id = id
//        self.chatType = chatType
//        switch chatType {
//        case .singleChat:
//            if id == EMSYSTEMNOTIFICATIONID {
//                self.name = "系统提示"
//                self.headImageName = "systemNotify"
//            }else{
//                self.headImageName = "defaultAvatar"
//            }
//            break
//        case .groupChat:
//            let group = EMGroup(id: id)
//            self.name = group?.groupName ?? "群id:\(id)"
//            self.headImageName = "groupConversation"
//            break
//        case .chatRoomChat:
//            self.name = "聊天室:\(id)"
//            self.headImageName = "chatroomConversation"
//            break
//        }
//    }
}
